package localizer.main;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class HelpActivity extends Activity implements OnClickListener, OnGestureListener{
	ViewFlipper flippy;
	Button btnNext;
	Button btnPrevious;
	private GestureDetector gesturedetector = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.help);
		flippy = (ViewFlipper)findViewById(R.id.viewSwitcher);


		btnNext = (Button) findViewById(R.id.btnNext);
		btnPrevious = (Button) findViewById(R.id.btnPrevious);

		btnNext.setOnClickListener(this);
		btnPrevious.setOnClickListener(this);
		
		gesturedetector = new GestureDetector(this, this);
	}
	@Override
	public void onClick(View v) {
		switch(v.getId())
		{
		case R.id.btnNext:
			Animation animationNext = AnimationUtils.loadAnimation(this, R.anim.btn_style_next); 
			btnNext.startAnimation(animationNext);

			nextView();
			break;

		case R.id.btnPrevious:
			Animation animationPrevious = AnimationUtils.loadAnimation(this, R.anim.btn_style_previous);
			btnPrevious.startAnimation(animationPrevious);

			previousView();
			break;
		}
	}
	private void nextView() {
		flippy.setInAnimation(inFromRightAnimation());
		flippy.setOutAnimation(outToLeftAnimation());
		flippy.showNext();
	}
	private void previousView() {
		flippy.setInAnimation(inFromLeftAnimation());
		flippy.setOutAnimation(outToRightAnimation());
		flippy.showPrevious();

	}

	private final int ANIMATION_DURATION = 500;
	protected Animation inFromRightAnimation() {

		Animation inFromRight = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromRight.setDuration(ANIMATION_DURATION);
		inFromRight.setInterpolator(new LinearInterpolator());
		return inFromRight;
	}

	protected Animation outToLeftAnimation() {
		Animation outtoLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoLeft.setDuration(ANIMATION_DURATION);
		outtoLeft.setInterpolator(new LinearInterpolator());
		return outtoLeft;
	}

	protected Animation inFromLeftAnimation() {
		Animation inFromLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromLeft.setDuration(ANIMATION_DURATION);
		inFromLeft.setInterpolator(new LinearInterpolator());
		return inFromLeft;
	}

	protected Animation outToRightAnimation() {
		Animation outtoRight = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoRight.setDuration(ANIMATION_DURATION);
		outtoRight.setInterpolator(new LinearInterpolator());
		return outtoRight;
	}

	
	@Override
    public boolean onTouchEvent(MotionEvent event) {
			return gesturedetector.onTouchEvent(event);
    }
	@Override
	public boolean onDown(MotionEvent e) {
		return false;
	}
	
	int SWIPE_MIN_VELOCITY = 50;
	int SWIPE_MIN_DISTANCE = 50;
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
		float ev1X = e1.getX();
		float ev2X = e2.getX();
		final float xdistance = Math.abs(ev1X - ev2X);
        final float xvelocity = Math.abs(velocityX);
        
        if( (xvelocity > SWIPE_MIN_VELOCITY) && (xdistance > SWIPE_MIN_DISTANCE) )
        {
			if(ev1X > ev2X){
				nextView();
			}else{
				previousView();
			}
        }
        
		return false;
	}
	@Override
	public void onLongPress(MotionEvent e) {
		
	}
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {
		return false;
	}
	@Override
	public void onShowPress(MotionEvent e) {
		
	}
	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}
}
